// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

$(function() {

	$("input#payment_tx_code").change(function(e) {
		e.preventDefault();
		// this debugger should be hit when you click the submit button!
		var data = "so_transaction[code]="+$(this).val() +"";

		$.ajax({
			url : '/so_transactions',
			type : 'get',
			data : data,
			dataType : 'json',
			success : function(data) {
				
				var node = $("div.payment_tx_code").find("div.invalid-feedback");
				
				if (data.length == 0 ) {					
					if (! node.length ) {
						$("input#payment_tx_code").addClass("is-invalid");
						$("input#payment_tx_code").after("<div class=\"invalid-feedback\">客户付款流水单号 不存在或者不正确！</div>");
					};
					
					return false;
				}		
					
				if (data.length == 1 ) {
					
					if ( node.length ) {
						$("input#payment_tx_code").removeClass("is-invalid");
						$("div.payment_tx_code").find("div.invalid-feedback").remove();						
					};
					
					if ( $('input#payment_tx_id').length ) {
						$('input#payment_tx_id').remove();
					};
					
					$("input#payment_tx_code").after("<input type=\"hidden\" id=payment_tx_id name=payment[so_transaction_id] value="+data[0]["id"]+">");
													
					return;
				}
			},
			error : function(e) {
				console.log('error'+data);
			}
		});

		// $.getJSON(action,data, function(data) {
		// alert("hi"+data);
		// });
	});

});

